package study.jsm.com.myapplication;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import study.jsm.com.myapplication.Utils.DBHelper;
import study.jsm.com.myapplication.Utils.StingUtils;
import study.jsm.com.myapplication.Utils.ToastUtils;

public class RegisterActivity extends Activity {
    EditText et_account;
    EditText et_psd;
    EditText et_psd_check;
    EditText et_age;
    TextView tv_register;

    DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbHelper = new DBHelper(this);

        et_account = findViewById(R.id.et_account);
        et_psd = findViewById(R.id.et_psd);
        et_psd_check = findViewById(R.id.et_psd_check);
        tv_register = findViewById(R.id.tv_register);
        et_age = findViewById(R.id.et_age);

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account = et_account.getText().toString();
                String psd = et_psd.getText().toString();
                String psdCheck = et_psd_check.getText().toString();
                int age= Integer.parseInt(et_age.getText().toString());
                if (psd.equals(psdCheck)) {

                    ContentValues contentValues = new ContentValues();
                    contentValues.put(DBHelper.FIELD_NAME,account);
                    contentValues.put(DBHelper.FIELD_PASSWORD,psd);
                    contentValues.put(DBHelper.FIELD_AGE,age);
                    long id = dbHelper.addUser(contentValues);
                    if (id>0){
                        ToastUtils.showToast(getApplicationContext(),"注册成功");
                        finish();
                    }
                    else {
                        ToastUtils.showToast(getApplicationContext(),"注册失败");
                    }
                }
                else {
                    Toast.makeText(getApplicationContext(),"密码不一致，注册失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void clickClose(View view) {
        Intent intent = new Intent();
        intent.putExtra("result","我是返回值");
        setResult(200,intent);
        finish();
    }
}
